package pers.cz.logtail.handle;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import pers.cz.jefconfig.config.JefConfiguration;
import pers.cz.logtail.util.LogConfigItem;
import pers.cz.netty.handle.ServerMessageHandler;
import pers.cz.netty.protocal.Message;
import pers.cz.logtail.util.FileUtils;

/**
 * @program: PostGirl-panent
 * @description: 日志处理类
 * @author: Cheng Zhi
 * @create: 2022-10-05 11:41
 **/
@ChannelHandler.Sharable
public class LogHandle extends ServerMessageHandler {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Message message) throws Exception {
       try {
           String content = (String) message.getContent();
           //String cont = FileUtils.decode(content, "POST_GIRL");
           //System.out.println("接收到消息：" + content);
           // 写入文件
           String fileName = JefConfiguration.get(LogConfigItem.LOG_FILEPATH);
           FileUtils.writeFileByStreamByRandomAccess(fileName, content);
           //FileUtils.writeFileByStream(fileName, content);
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
}
